package gorails.com;




import java.util.ArrayList;


import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PickLine extends ListActivity {
	private ListView lv;
	private DBAdapter dbAdapter;
	private static final String TABLE_NAME = "line";
	private static final String COLUMN_1 = "metra";
	private ArrayAdapter<String> adapter;
	private LineListAdapter data;
	private ArrayList<String> list;
	private int metraSize = 0;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lv = getListView();
        lv.setTextFilterEnabled(true);

        list = getLine();
        
        data = new LineListAdapter(this, R.id.listitem,list);
        adapter = data;
		setListAdapter(adapter);

			
    }
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String keyword = o.toString();
		
		Intent i = new Intent(PickLine.this, PickOrigin.class);
		Trip.getInstance().setLine(keyword);
		String sqlLine = keyword.replace(" ", "");
		Trip.getInstance().setLinePosition(position);
		Trip.getInstance().setSQLLine(sqlLine);
		startActivity(i);
		finish();
	}
	@Override
	protected void onStart(){        
		setListAdapter(adapter);		
		super.onStart();
	}
	public ArrayList<String> getLine(){    	    	
		dbAdapter = new DBAdapter(this);
		SQLiteDatabase db =dbAdapter.getDB();
		ArrayList<String> LineList = new ArrayList<String>();
		String[] columns = new String[]{COLUMN_1};
		Cursor c = db.query(false, TABLE_NAME, columns, null , null, null, null, null, null);
		c.moveToPosition(-1);
		int i = 0;
		while(c.moveToNext()){				
			for(int a = 0; a < c.getColumnCount();a++){
				LineList.add(c.getString(a));	        	 
				Log.d("PickLine", c.getString(a));
			}
			i++;
		}
		
	    if (c != null && !c.isClosed()) {
	         c.close();
	    }
	    db.close();
		return LineList;
	}
	
	
}
